package com.t9101DR.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.t9101DR.bean.RoomAddress;
import com.t9101DR.dao.BaseDao;
import com.t9101DR.dao.RoomAddressDao;


public class RoomAddressDaoImpl extends BaseDao implements RoomAddressDao{

	@Override
	public List<RoomAddress> list(RoomAddress ra) {
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		List<RoomAddress> list=new ArrayList<RoomAddress>();
		try {
			conn=getConnection();
			String sql="SELECT addressId,addName,addArea,`Add`,phone,defaultAdd FROM `roomaddress` WHERE 1=1";
			if(ra!=null&&ra.getAddressId()!=0) {
				sql=sql+" and addressId= "+ra.getAddressId()+" ";
			}
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while(rs.next()) {
				RoomAddress bren=new RoomAddress();
				bren.setAddressId(rs.getInt(1));
				bren.setAddName(rs.getString(2));
				bren.setAddArea(rs.getString(3));
				bren.setAdd(rs.getString(4));
				bren.setPhone(rs.getInt(5));
				bren.setDefaultAdd(rs.getBoolean(6));
				list.add(bren);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			closeAll(conn, pstmt, rs);
		}
		return list;
	}

	@Override
	public int add(RoomAddress ra) {
		int count =0;
		String sql="INSERT INTO roomaddress(addName,addArea,`Add`,phone,defaultAdd) VALUES (?,?,?,?,?) ";
		Object [] pare= {ra.getAddName(),ra.getAddArea(),ra.getAdd(),ra.getPhone(),ra.getDefaultAdd()};
		count=executeUpdate(sql, pare);
		return count;
	}

	@Override
	public int update(RoomAddress ra) {
		
		int count =0;
		String sql=" UPDATE roomaddress set addName=?,addArea=?,`Add`=?,phone=?,defaultAdd=? WHERE addressId=?  ";
		Object [] params = {ra.getAddName(),ra.getAddArea(),ra.getAdd(),ra.getPhone(),ra.getDefaultAdd(),ra.getAddressId()};
		 count =this.executeUpdate(sql, params);
			return count;
	}

	@Override
	public int deleteById(int id) {
		int count=0;
		String sql="delete from roomaddress where addressId=?";
		Object[] params= {id};
		count=this.executeUpdate(sql, params);
		return count;
	}


}
